package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Reference
    PermissionService permissionService;
//权限新增
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        log.debug("PermissionController:add : permission:" +permission);
        permissionService.add(permission);
        log.debug("添加权限成功");
        //新增成功
        return new Result(true, "添加权限成功");
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("分页查询{"+queryPageBean+"}");
        PageResult pageResult = permissionService.findByPage(queryPageBean);
        log.debug("权限分页查询成功!!");
        return new Result(true, "权限分页查询成功!!", pageResult);
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("权限删除:delete: " + id);
        permissionService.delById(id);
        log.debug("权限删除成功!!");
        return new Result(true,"权限删除成功!!");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("permissionService:findById: " + id);
        Permission permission = permissionService.findById(id);
        log.debug("权限查询成功!!");
        return new Result(true,"权限查询成功!!",permission);
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] checkitemIds ,@RequestBody Permission permission){
        log.debug("PermissionController:edit: " + permission);
        permissionService.edit(permission);
        log.debug("权限修改成功！！！");
        return new Result(true,"权限修改成功！！！");

    }


    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("MenuController:findAll: " );
        List<CheckGroup> checkGroupList = permissionService.findAll();
        log.debug("权限组查询成功！！！");
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }
}
